<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 22:38
 */
namespace app\api\model;
use think\Model;
use think\facade\Url;
use think\Loader;
use phpqrcode\QRcode;

class MemberTmp extends Base
{
    
    
    /**
    * 通过udid获取临时表数据
    * @param udid string 设备UDID
    * @return array 临时用户信息
    */
    public function _getInfoByUdid($udid){
        return $this->where([['udid', '=', $udid]])->find();
    }
    
    /**
    * 通过token获取游客
    * @param token string 游客token
    * @return array 游戏信息
    */
    public function _getInfoByToken($token){
        return $this->where([['token', '=', $token]])->find();
    }
    
    /**
    * 通过id获取游客信息
    * @param id int 游客id
    * @return array 游戏信息
    */
    public function _getInfoById($id){
        return $this->where([['id', '=', $id]])->find();
    }
    
    /**
    * 通过udid创建临时用户
    * @param udid 设备UDID
    */
    public function _createTmp($udid){
        $token = createTmpToken(0);
        $config = $this->getConfig();
        $deadline = time() + $config['TOEKN_TIME'] * 60;
        
        $image = $config['DEFAULT_HEAD'];
        $unique = strtoupper(nocstr(9));
        $insert_data = [
            'udid' => $udid,
            'token' => $token,
            'deadline' => $deadline,
            'unique' => $unique,
            'image' => $image,
            'nick_name' => rand_name(),
            'login_time' => time(),
            'created' => time(),
        ];
        //var_dump($insert_data);die;
            
        $ret = $this->save($insert_data);
        if(!$ret){
            $this->error = '创建失败';
            return false;
        }
        return true;
    }
    
    /**
    * 通过udid刷新临时表token
    */
    public function _refreashToken($udid){
        $data =  $this->where([['udid', '=', $udid]])->find();
        
        $token = createTmpToken(0);
        $config = $this->getConfig();
        $deadline = time() + $config['TOEKN_TIME'] * 60;
        
        $data->token = $token;
        $data->deadline = $deadline;
        $data->login_time = time();
        $ret = $data->save();
        if(!$ret){
            $this->error = 'token创建失败';
            return false;
        }
        
        return true;
    }
    

    
    /**
    * 校验token
    * @param token string 用户token
    * @return bool
    */
    public function _checkTmpToken($token){
        if(empty($token)){
            $this->error = '缺少token';
            return false;
        }
        //var_dump(time());die;
        $data = $this->where([['token', '=', $token], ['deadline', '>=', time()]])->find();
        if(empty($data)){
            $this->error = 'token已过期';
            return false;
        }
        return true;
    }
    
    /**
    * 将游客的状态改为激活状态
    * @param udid string ios 设备UDID
    */
    public function _activeTmp($udid){
        $data = $this->where([['udid', '=', $udid], ['active', '=', -1]])->find();
        if(empty($data)){
            $this->error = '该账号已激活';
            return false;
        }
        
        $data->active = 1;
        $ret = $data->save();
        if(!$ret){
            $this->error = '账号激活失败';
            return false;
        }
        
        return true;
    }
    
}